# coding: utf-8
import requests
from dingtalkchatbot.chatbot import DingtalkChatbot

from ..config import pytest_config


class DingDingClient:
    base_url = " https://oapi.dingtalk.com/robot/send"

    def __init__(self, **kwargs):
        """
        机器人初始化
        :param webhook: 钉钉群自定义机器人webhook地址
        :param secret: 机器人安全设置页面勾选“加签”时需要传入的密钥
        :param pc_slide: 消息链接打开方式，默认False为浏览器打开，设置为True时为PC端侧边栏打开
        :param fail_notice: 消息发送失败提醒，默认为False不提醒，开发者可以根据返回的消息发送结果自行判断和处理
        """
        webhook = kwargs.get('webhook', pytest_config.ding_ding_setting.access_token)
        secret = kwargs.get('secret', pytest_config.ding_ding_setting.secret)
        pc_slide = kwargs.get('pc_slide', False)
        fail_notice = kwargs.get('fail_notice', False)
        self.dingtalk_bot = DingtalkChatbot(webhook, secret=secret, pc_slide=pc_slide, fail_notice=fail_notice)

    def send(self, text: str) -> requests.Response:
        """
        发送消息
        :return:
        """
        project_name = pytest_config.auto_test_setting.project_name
        return self.dingtalk_bot.send_markdown(title=project_name, text=text)
